package com.xiaozhou.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.xiaozhou.mapper.StockMarketIndexMapper;
import com.xiaozhou.pojo.dto.Stock4EvrDayDomain;
import com.xiaozhou.pojo.dto.Stock4MinuteDomain;
import com.xiaozhou.pojo.dto.StockUpdownDomain;
import com.xiaozhou.pojo.vo.*;
import com.xiaozhou.result.PageResult;
import com.xiaozhou.result.Result;
import com.xiaozhou.service.StockMarketIndexService;
import com.xiaozhou.utils.DateTimeUtil;
import lombok.extern.slf4j.Slf4j;
import org.joda.time.DateTime;
import org.joda.time.format.DateTimeFormat;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * CTassName: StockMarketIndexServiceImpl
 * Package: com.xiaozhou.service.impl
 * Description:
 * 国内⼤盘指数功能
 */

@Slf4j
@Service
public class StockMarketIndexServiceImpl implements StockMarketIndexService {


    /**
     * ⼤盘指数功能 业务层
     */
    @Autowired
    private StockMarketIndexMapper stockMarketIndexMapper;


    /**
     * 大盘板块
     * 国内⼤盘指数功能
     */
    @Override
    public List<StockMarketIndexInfoVo> getalldata() {
        List<StockMarketIndexInfoVo> getalldata = stockMarketIndexMapper.getalldata();
        log.info("---> {}", getalldata);

        return stockMarketIndexMapper.getalldata();
    }

    /**
     * 板块指数功能实现
     */
    @Override
    public List<StockBlockRtInfoVo> sectorAllLimit() {
        log.info("板块指数功能实现 Service");

        List<StockBlockRtInfoVo> stockBlockRtInfoVos = stockMarketIndexMapper.sectorAllLimits();
        log.info("---> {}", stockBlockRtInfoVos);


        return stockBlockRtInfoVos;
    }


    /**
     * 分页降序查询最新的个股涨幅数据
     */
    @Override
    public Result<PageResult<StockUpdownDomain>> getPageStockInfos(Integer page, Integer pageSize) {


        // 开启分页
        PageHelper.startPage(page, pageSize);

        // 调用mapper查询数据
        List<StockBlockRtInfoVo> stockBlockDomains = stockMarketIndexMapper.selectStockRtInfo();

        // 将数据分页封装
        PageInfo pageInfo = new PageInfo(stockBlockDomains);

        PageResult<StockUpdownDomain> stockUpdownDomainPageResult = new PageResult<StockUpdownDomain>(pageInfo);

        log.info(stockUpdownDomainPageResult.toString());


        return Result.success(stockUpdownDomainPageResult);
    }


    /**
     * 涨幅榜功能实现
     */
    @Override
    public List<StockInfo> getIncrease() {
        return stockMarketIndexMapper.getIncrease();
    }


    /**
     * 涨停跌停数据统计功能
     */
    @Override
    public StockData getUpdownCount() {
        List<UpList> upListStockInfo = stockMarketIndexMapper.getUpdownCount();
        List<UpList> downListStockInfo = stockMarketIndexMapper.getDownListCount();
        StockData stockData = new StockData();
        stockData.setUpList(upListStockInfo);
        stockData.setDownList(downListStockInfo);
        return stockData;
    }


    /**
     * 股票成交量对⽐功能
     */
    @Override
    public Result<Map<String, List>> getTradeAmt() {

        // 1.获得T日的股票交易范围
        // T日的截止时间
        DateTime now = DateTime.now();

        DateTime lastDate4Stock = DateTimeUtil.getLastDate4Stock(now);

        Date endTiem = lastDate4Stock.toDate();


        // 开盘时间
        DateTime openDate = DateTimeUtil.getOpenDate(lastDate4Stock);
        Date startTime = openDate.toDate();

        endTiem = DateTime.parse(
                "2022-01-03 14:40:00",
                DateTimeFormat.forPattern("yyyy-mm-dd HH:mm:ss")).toDate();


        startTime = DateTime.parse(
                "2022-01-03 09:30:00",
                DateTimeFormat.forPattern("yyyy-mm-dd HH:mm:ss")).toDate();


        List<Map> tiofos = stockMarketIndexMapper.getTradeAmt(startTime, endTiem);


        // t-1
        DateTime previousTradingDay = DateTimeUtil.getPreviousTradingDay(now);

        Date preEndTime = previousTradingDay.toDate();

        Date preStartTime = DateTimeUtil.getOpenDate(previousTradingDay).toDate();

        preEndTime = DateTime.parse(
                "2022-01-02 14:40:00",
                DateTimeFormat.forPattern("yyyy-mm-dd HH:mm:ss")).toDate();


        preStartTime = DateTime.parse(
                "2022-01-02 09:30:00",
                DateTimeFormat.forPattern("yyyy-mm-dd HH:mm:ss")).toDate();


        List<Map> preiofos = stockMarketIndexMapper.getTradeAmt(preStartTime, preEndTime);


        HashMap<String, List> stringListHashMap = new HashMap<>();
        stringListHashMap.put("amtList", tiofos);
        stringListHashMap.put("yesAmtList", preiofos);


        return Result.success(stringListHashMap);
    }


    /**
     * 个股分时涨跌幅度统计功能
     */
    @Override
    public Result<Map> stockUpdown() {

        List<Map<String, Integer>> list = stockMarketIndexMapper.stockUpdown();
        log.info("个股分时涨跌幅度统计功能：返回数据，{}", list);


        // 包装返回值
        Map<String, Object> map = new HashMap<>();
        map.put("time", "2021-12-31 14:58:00"); // 添加时间字段
        map.put("infos", list);


        return Result.success(map);
    }

    /**
     * 个股分时图行情功能
     */
    @Override
    public Result<List<Stock4MinuteDomain>> stockScreenTimeSharing(String code) {

        // 固定的开始和结束时间
        Date startTime = getDateTimeFromString("2021-12-31 09:32:00");
        Date endTime = getDateTimeFromString("2021-12-31 14:58:00");

        List<Stock4MinuteDomain> list = stockMarketIndexMapper.stockScreenTimeSharing(
                code,
                startTime,
                endTime);

        return Result.success(list);
    }


    private Date getDateTimeFromString(String dateTimeString) {
        // 这里使用Java 8+的日期时间API进行转换
        return Date.from(LocalDateTime.parse(dateTimeString, DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")).atZone(ZoneId.systemDefault()).toInstant());
    }


    /**
     * 股票K线图功能
     */
    @Override
    public Result<List<Stock4EvrDayDomain>> stockCreenDkLine(String stockCode) {

        // 固定的开始和结束时间
        Date startTime = getDateTimeFromString("2021-12-22 09:32:00");
        Date endTime = getDateTimeFromString("2022-01-06 14:25:00");


        List<Stock4EvrDayDomain> list = stockMarketIndexMapper.getStockInfo4EvrDay(
                stockCode,
                startTime,
                endTime);

        log.info("list：{}",list);

        return Result.success(list);
    }

}
